unit uMainForm;

interface

uses
  Windows, SysUtils, Classes, Forms,

  GlScene, GlObjects, GlCadencer, GLWin32Viewer, GLRenderContextInfo, GlWindowsFont, GlHudObjects,
  VectorGeometry, GlMaterial,
  uSimpleListBox;
const
  ImageFolder = 'NoScrollbars';
type
  TfrmMain = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    {Main Objects}
    fGlScene  : TGlScene;
    fCadencer : TGLCadencer;
    fViewer   : TGLSceneViewer;
    fCamera   : TGlCamera;
    fMainDummy: TGlDummyCube;
    fRenderer : TGLDirectOpenGL;
    fMatLib   : TGLMaterialLibrary;
    fFpsTimer : Single;
    fMousePressed: Boolean;
    fSpacePressed: Boolean;
    fFullScreen: Boolean;

    {Text Objects}
    fWinFont: TGLWindowsBitmapFont;
    fHudText: TGLHUDText;
    fFPSText: TGLHUDText;
    fBack   : TGLHUDSprite;
    fTitle  : TGLHUDSprite;

    {List Box}
    fListBox: TSimpleListBox;
  public
    Procedure SwitchScreenMode(aFullScreenMode: Boolean);
    Procedure InitFont;
    Procedure InitMaterials;
    Procedure CreateListBox;
    Procedure OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
  end;

var
  frmMain: TfrmMain;

implementation
uses
  GlTexture, GlCompositeImage, GlFilePng, GlFileJPEG, GlKeyboard,
  uDemoInit;

{$R *.dfm}
//------------------------------------------------------------------------------
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  fFpsTimer := 0;
  CreateDemo(self, fGlScene, fCadencer, fViewer);
  InitDemo(fGlScene, fCamera, fMainDummy, fRenderer);
  fViewer.Camera := fCamera;
  fCadencer.OnProgress := OnCadencerProgress;
  fCadencer.Enabled := true;

  InitFont;
  InitMaterials;
  CreateListBox;
  fHudText.MoveLast;
  fFullScreen := false;
end;
//------------------------------------------------------------------------------
Procedure TfrmMain.SwitchScreenMode(aFullScreenMode: Boolean);
const
  frmWidth = 808;
  frmHeight = 634;
var
  Style: Cardinal;
begin
  Style := GetWindowLong(Handle, GWL_STYLE);
  if not aFullScreenMode then
  begin
    SetWindowLong(Handle, GWL_STYLE, Style or WS_BORDER or WS_Caption);
    WindowState := wsNormal;
    SetWindowPos(Handle, HWND_TOP, Left, Top, frmWidth, frmHeight, SWP_SHOWWINDOW or SWP_FRAMECHANGED);
  end
  else
  begin
    SetWindowLong(Handle, GWL_STYLE, Style and not WS_BORDER and not WS_Caption);
    SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_SHOWWINDOW or SWP_FRAMECHANGED);
    WindowState := wsMaximized;
  end;
  fFullScreen := aFullScreenMode;

  fBack.Position.X := Width / 2;
  fBack.Position.Y := Height / 2;
  fBack.Width := Width;
  fBack.Height := Height;
  fHudText.Position.SetPoint(ClientWidth - 10, ClientHeight - 40, 0);
end;
//------------------------------------------------------------------------------
Procedure TfrmMain.InitFont;
begin
  fWinFont := TGLWindowsBitmapFont.Create(self);
  fWinFont.Font.Name := 'Arial';
  fWinFont.Font.Size := 8;
  fHudText := TGLHUDText.CreateAsChild(fMainDummy);
  with fHudText do
  begin
    BitmapFont := fWinFont;
    Text := 'Lampogolovii presents... ''No scrollbars'' Demo!' + #13#10 +
            'press Space to change view mode!';
    Position.SetPoint(790, 560, 0);
    Alignment := taRightJustify;
  end;

  fFPSText := TGLHUDText.CreateAsChild(fMainDummy);
end;
//------------------------------------------------------------------------------
Procedure TfrmMain.InitMaterials;
  Procedure AddMaterial(fFileName, fMaterialName: String);
  begin
    with fMatLib.Materials.Add do
    begin
      with Material do
      begin
        MaterialOptions := [moIgnoreFog, moNoLighting];
        Texture.Disabled := false;
        BlendingMode := bmTransparency;
        Texture.TextureMode := tmModulate;
        with FrontProperties do
        begin
          Ambient. SetColor(1,1,1, 1);
          Diffuse. SetColor(1,1,1, 1);
          Emission.SetColor(1,1,1, 1);
          Specular.SetColor(1,1,1, 1);
        end;
        Texture.ImageClassName := 'TGLCompositeImage';
        TGLCompositeImage(Texture.Image).LoadFromFile(str_ImagesFolder + '\' + ImageFolder + '\' + fFileName);
      end;
      Name := fMaterialName;
    end;
  end;
begin
  fMatLib := TGLMaterialLibrary.Create(Self);
  AddMaterial('back_1.jpg', 'back');
  AddMaterial('title.png', 'title');

  AddMaterial('back_panel.png', 'back_panel');
  AddMaterial('front_panel.png', 'front_panel');
  AddMaterial('selection.png', 'selection_panel');
end;
//------------------------------------------------------------------------------
Procedure TfrmMain.CreateListBox;
var
  i: integer;
  Function CreateHud(aName: String; x, y: integer; w, h: integer): TGlHudSprite;
  begin
    result := TGLHUDSprite.CreateAsChild(fMainDummy);
    with result do
    begin
      Material.LibMaterialName := aName;
      Material.MaterialLibrary := fMatLib;
      Position.X := x;
      Position.Y := y;
      Width := w;
      Height := h;
    end;
  end;
begin
  fBack  := CreateHud('back', 400, 300, 800, 600);

  fListBox := TSimpleListBox.CreateAsChild(fMainDummy, fMatLib, fWinFont);
  fListBox.Position.X := 150;
  fListBox.Position.Y := 300;
  for i := 1 to 30 do
    fListBox.AddItemText(inttostr(i) + '. Unknown Artist');

  fTitle := CreateHud('title', 150, 110, 128, 64);
end;
//------------------------------------------------------------------------------
Procedure TfrmMain.OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
var
  MPos: TPoint;
begin
  fViewer.Invalidate;

  if not fSpacePressed and IsKeyDown(vk_Space) then
    SwitchScreenMode(not fFullScreen);

  GetCursorPos(MPos);
  MPos := fViewer.ScreenToClient(MPos);
  fListBox.SetMousePosition(MPos.x, MPos.y);
  fMousePressed := IsKeyDown(vk_LButton);
  fSpacePressed := IsKeyDown(vk_Space);
end;
end.
